package com.yanqu.road.server.cmd.group.rank;

import com.yanqu.road.logic.cross.CrossGroupRankListModel;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.rank.CrossRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = CrossProtocol.C_CROSS_GROUP_RANK, desc = "分组榜单数据")
public class GetCrossGroupRankCmd extends CrossCommand {
    private long userId;
    private int groupId;
    private int type;
    private int gameType;
    private int bigType;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RankProto.CrossGroupRankReqMsg req = RankProto.CrossGroupRankReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        groupId = req.getGroupId();
        type = req.getCorssType();
        bigType = req.getBigRankType();
        gameType = req.getGameType();
        if(0 == groupId){
            return false;
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        Map<Integer, Map<Integer, Map<Integer, CrossGroupRankListModel>>> modelMap = CrossRankMgr.getAllCrossGroupRankListModelMap();
        Map<Integer, Map<Integer, CrossGroupRankListModel>> rankMap = modelMap.get(groupId);
        if(null == rankMap){
            log.error("GroupRankList error,serverId:{},userId:{},groupId:{},bigType:{},type:{}", serverId, userId, groupId, bigType, type);
            return;
        }
        CrossGroupRankListModel rankListModel = rankMap.get(bigType).get(type);
        RankProto.RankListMsg.Builder rankListMsg = rankListModel.getRankListMsg();

        RankProto.RankListRespMsg.Builder respMsg = RankProto.RankListRespMsg.newBuilder();
        respMsg.setBigRankType(bigType);
        respMsg.setRankType(gameType);
        respMsg.setRankList(rankListMsg);
        int myRank = rankListModel.getMyRank(userId, serverId);
        respMsg.setMyRank(myRank);
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_USER_RANK_LIST, respMsg);
        MessageHelper.sendPacket(serverId, userId, message);


    }

    @Override
    public void sendData() {

    }
}
